package com.resou.reader.signin.bindingtask;

import android.text.TextUtils;
import android.util.Log;

import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.Injection;
import com.resou.reader.data.signin.SignInRepository;
import com.resou.reader.data.signin.model.BindingTask;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.UserUtil;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BindingTaskPresenter extends ResouBasePresenter<BindingTaskView> {
    private static final String TAG = "BindingTaskPresenter";
    private final SignInRepository mRepository;

    public BindingTaskPresenter(BindingTaskView view) {
        super(view);
        mRepository = Injection.provideSignInRepository();
    }

    /**
     * 加载绑定任务
     */
    void loadBingTask() {
        String token = UserUtil.getToken();
        if (TextUtils.isEmpty(token)) {
            ToastUtil.makeShortToast("您尚未登录，请先登录后重试");
            return;
        }
        Log.d(TAG, "loadBingTask: " + token);
        addCompositeDisposable(
                mRepository.getVipTask(token)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(resultList -> {
                            if (resultList.getCode() == 0) {
                                List<BindingTask> tasks = resultList.getData();
                                mView.setTasks(tasks);
                            }
                        }, throwable -> mView.showError(throwable))
        );
    }


}
